<?php

namespace app\admin\model\chase;

use think\Model;
use traits\model\SoftDelete;

class Coupon extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'coupons';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'discount_type_text',
        'online_text',
        'expired_type_text',
        'buy_type_text'
    ];
    

    
    public function getDiscountTypeList()
    {
        return ['1' => __('Discount_type 1'), '2' => __('Discount_type 2'), '3' => __('Discount_type 3'), '4' => __('Discount_type 4')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '4' => __('Type 4')];
    }

    public function getOnlineList()
    {
        return ['0' => __('Online 0'), '1' => __('Online 1')];
    }

    public function getExpiredTypeList()
    {
        return ['1' => __('Expired_type 1'), '2' => __('Expired_type 2')];
    }

    public function getBuyTypeList()
    {
        return ['1' => __('Buy_type 1'), '2' => __('Buy_type 2')];
    }


    public function getDiscountTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['discount_type']) ? $data['discount_type'] : '');
        $list = $this->getDiscountTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getDiscountTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOnlineTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['online']) ? $data['online'] : '');
        $list = $this->getOnlineList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExpiredTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expired_type']) ? $data['expired_type'] : '');
        $list = $this->getExpiredTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBuyTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['buy_type']) ? $data['buy_type'] : '');
        $list = $this->getBuyTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function category()
    {
        return $this->hasOne(CouponCategory::class, 'id', 'category_id');
    }

    public function service()
    {
        return $this->hasOne(ModelingService::class, 'id', 'service_id');
    }

    public static function getCoupons()
    {
        return self::order('weigh desc')->select();
    }

    public function vip()
    {
        return $this->belongsToMany(UserVip::class, 'coupon_vip', 'user_vip_id', 'coupon_id');
    }

}
